package com.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.annotation.AuthCheck;
import com.study.common.BaseResponse;
import com.study.common.DeleteRequest;
import com.study.common.ResultUtils;
import com.study.constant.UserConstant;
import com.study.exception.ErrorCode;
import com.study.exception.ThrowUtils;
import com.study.model.dto.note.LearningNoteAddRequest;
import com.study.model.dto.note.LearningNoteQueryRequest;
import com.study.model.dto.note.LearningNoteUpdateRequest;
import com.study.model.dto.user.*;
import com.study.model.entity.LearningNote;
import com.study.model.entity.User;
import com.study.model.vo.LearningNoteVO;
import com.study.model.vo.UserLoginVO;
import com.study.model.vo.UserVO;
import com.study.service.LearningNoteService;
import com.study.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 学习笔记控制器
 */
@RestController
@RequestMapping("/note")
public class LearningNoteController {

    @Resource
    private LearningNoteService learningNoteService;

    @Resource
    private UserService userService;

    /**
     * 创建笔记
     */
    @PostMapping("/add")
    public BaseResponse<Long> addNote(@RequestBody LearningNoteAddRequest noteAddRequest,HttpServletRequest request) {
        ThrowUtils.throwIf(noteAddRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(learningNoteService.createLearningNote(noteAddRequest,request));
    }

    /**
     * 根据 id 获取笔记（仅管理员）
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<LearningNote> getLearningNoteById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        LearningNote note = learningNoteService.getById(id);
        ThrowUtils.throwIf(note == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(note);
    }

    /**
     * 根据 id 获取包装类
     */
    @GetMapping("/get/vo")
    public BaseResponse<LearningNoteVO> getLearningNoteVOById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        LearningNoteVO noteVO=learningNoteService.getLearningNoteVOById(id);
        return ResultUtils.success(noteVO);
    }

    /**
     * 删除学习笔记
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteLearningNote(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(learningNoteService.removeById(deleteRequest.getId()));

    }

    /**
     * 更新学习笔记
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateLearningNote(@RequestBody LearningNoteUpdateRequest noteUpdateRequest) {
        ThrowUtils.throwIf(noteUpdateRequest == null || noteUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(learningNoteService.updateLearningNote(noteUpdateRequest));

    }

    /**
     * 分页获取学习笔记封装列表（仅管理员）
     *
     * @param noteQueryRequest 查询请求参数
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<LearningNoteVO>> listLearningNoteVOByPage(@RequestBody LearningNoteQueryRequest noteQueryRequest) {
        ThrowUtils.throwIf(noteQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(learningNoteService.listLearningNoteVOByPage(noteQueryRequest));
    }

    /**
     * 分页获取学习笔记封装列表（普通用户）
     *
     * @param noteQueryRequest 查询请求参数
     */
    @PostMapping("/list/page/vo/user")
    public BaseResponse<Page<LearningNoteVO>> listLearningNoteVOByPageUser(@RequestBody LearningNoteQueryRequest noteQueryRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(noteQueryRequest == null, ErrorCode.PARAMS_ERROR);
        UserLoginVO loginUser = userService.getLoginUser(request);
        noteQueryRequest.setUserId(loginUser.getId());
        return ResultUtils.success(learningNoteService.listLearningNoteVOByPage(noteQueryRequest));
    }

}
